package system.controller;

import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import core.jfinal.ext.autoroute.ControllerBind;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import java.util.List;


/**
 * Created by Administrator on 2017/3/3.
 */
@ControllerBind(controllerKey = "/test")
public class TestController extends Controller{


    public void first(){
        Log.getLog(getClass()).info("info");
        Log.getLog(getClass()).debug("debug");
    }

    public void login(){

        UsernamePasswordToken token = new UsernamePasswordToken(getPara(0),getPara(1));
        Subject currentuser = SecurityUtils.getSubject();
        if(!currentuser.isAuthenticated()){
            try {
                currentuser.login(token);
            } catch (UnknownAccountException e) {
                renderText("账户不存在");
                return;
            } catch (IncorrectCredentialsException e){
                renderText("密码错误");
                return;
            }
        }else{
            renderText("已认证");
            return;
        }
        renderText("登录成功");
    }

    public void testGetSubject(){
        Subject subject = SecurityUtils.getSubject();
        Thread t =Thread.currentThread();
        System.out.println(t.getName());
        if(subject!=null){
            renderText(SecurityUtils.getSubject().getPrincipal().toString());
        }else{
            renderText("subject not exist");
        }
    }

    public void  testDB(){

        List<Record> users = Db.find("select * from user");

        renderText("11111111111111111111111");

    }

    public static void main(String[] args) {

        ThreadLocal tl = new ThreadLocal();

        for(int i =0;i<100;i++){
            Thread t3 = new Thread(new MyThread(tl));
            t3.start();

        }



    }





}
